package MONITORING::DB::MYSQL::REPLICATION;

#################################################################
# $LastChangedDate: 2013-11-25 15:03:06 +0000 (Mon, 25 Nov 2013) $
# $LastChangedRevision: 22 $
# $LastChangedBy: aswod_83@gmx.net $
#################################################################
use strict;
use base qw(moniCA::DBH::MySQL);
use XML::Simple;
use Data::Dumper;

#-------------------------------------------------------------------------------
sub Run {

#-------------------------------------------------------------------------------
# Description:
# Input Parms:
# Output Parms: None
#-------------------------------------------------------------------------------
    my $self      = shift;
    my $cnf       = $self->GetCNF();
    my $logger    = $self->GetLogger();
    my $statefile = $self->TmpDir() . '/' . $self->Plug() . '.xml';
    my $oldstat;
    my %newstat;

    if ( -s $statefile ) {
        $oldstat = XMLin( $statefile, ForceArray => 0 );
    }

    $self->_getSlave_status();

    if ( defined( $self->{slave_status} ) ) {

        $self->Report() if ( $cnf->{'Report'} =~ /true/i );

        if ( $self->get_bckp_stat() != 1 ) {

            if ( $self->{slave_status}->{Slave_IO_Running} ne "Yes" ) {
                $newstat{Slave_status}{Slave_IO_Running} =
                  ( $oldstat->{Slave_status}->{Slave_IO_Running} + 1 ) || 1;
                if ( $newstat{Slave_status}{Slave_IO_Running} ==
                    $cnf->{max_event_count} )
                {
                    $logger->error( "mysql io_thread: "
                          . $self->{slave_status}->{Slave_IO_Running} );
                }
                else {
                    $logger->warn( "mysql io_thread: "
                          . $self->{slave_status}->{Slave_IO_Running} );
                }
            }
            else {
                $newstat{Slave_status}{Slave_IO_Running} = 0;
                $logger->info( "mysql io_thread: "
                      . $self->{slave_status}->{Slave_IO_Running} );
            }

            if ( $self->{slave_status}->{Slave_SQL_Running} ne "Yes" ) {
                $newstat{Slave_status}{Slave_SQL_Running} =
                  ( $oldstat->{Slave_status}->{Slave_SQL_Running} + 1 ) || 1;
                if ( $newstat{Slave_status}{Slave_SQL_Running} ==
                    $cnf->{max_event_count} )
                {
                    $logger->error( "mysql sql_thread: "
                          . $self->{slave_status}->{Slave_SQL_Running} );
                }
                else {
                    $logger->warn( "mysql sql_thread: "
                          . $self->{slave_status}->{Slave_SQL_Running} );
                }
            }
            else {
                $newstat{Slave_status}{Slave_SQL_Running} = 0;
                $logger->info( "mysql sql_thread: "
                      . $self->{slave_status}->{Slave_SQL_Running} );
            }

            if ( defined( $self->{slave_status}->{Seconds_Behind_Master} ) ) {
                if ( $self->{slave_status}->{Seconds_Behind_Master} >
                    $cnf->{max_repl_delay} )
                {
                    $newstat{Slave_status}{Seconds_Behind_Master} =
                      ( $oldstat->{Slave_status}->{Seconds_Behind_Master} + 1 )
                      || 1;
                    if ( $newstat{Slave_status}{Seconds_Behind_Master} ==
                        $cnf->{max_event_count} )
                    {
                        $logger->error( "mysql repl_delay: "
                              . $self->{slave_status}->{Seconds_Behind_Master}
                        );
                    }
                    else {
                        $logger->warn( "mysql repl_delay: "
                              . $self->{slave_status}->{Seconds_Behind_Master}
                        );
                    }
                }
                else {
                    $newstat{Slave_status}{Seconds_Behind_Master} = 0;
                    $logger->info( "mysql repl_delay: "
                          . $self->{slave_status}->{Seconds_Behind_Master} );
                }
            }
            XMLout( \%newstat, outputfile => $statefile, NoSort => 0 );
        }
        else {
            $logger->debug("Backup is running!");
        }
    }
    else {
        $logger->debug("No Slave configured!");
    }
}

#-------------------------------------------------------------------------------
sub Report {

#-------------------------------------------------------------------------------
# Description:  Creating/Updating RRD File
# Input Parms:	Tablespace_Name
# Output Parms: None
#-------------------------------------------------------------------------------
    my $self   = shift;
    my $logger = $self->GetLogger();
    my $cnf    = $self->GetCNF();

    eval {
        require RRDs;

        if ( !-f $self->VarDir() . '/' . $self->Plug() . '.rrd' ) {
            RRDs::create(
                $self->VarDir() . '/' . $self->Plug() . '.rrd',
                "--step=$cnf->{ScheduleInterval}",
                "DS:SecondsBehindMaster:GAUGE:1200:0:100000000",
                "RRA:AVERAGE:0.5:1:800",
                "RRA:AVERAGE:0.5:6:800",
                "RRA:AVERAGE:0.5:24:800",
                "RRA:AVERAGE:0.5:288:800",
                "RRA:MAX:0.5:1:800",
                "RRA:MAX:0.5:6:800",
                "RRA:MAX:0.5:24:800",
                "RRA:MAX:0.5:288:800"
            );
            $logger->warn( "RRDs::create returns " . RRDs::error() )
              if ( RRDs::error() );
        }

        if ( -f $self->VarDir() . '/' . $self->Plug() . '.rrd' ) {
            RRDs::update(
                $self->VarDir() . '/' . $self->Plug() . '.rrd',
                '--template=SecondsBehindMaster',
                'N' . ':' . $self->{slave_status}->{Seconds_Behind_Master}
            );
            $logger->warn( "RRDs::update returns " . RRDs::error() )
              if ( RRDs::error() );
        }
        1;
    } or $logger->info( "Report incomplete: " . $@ );
}

#-------------------------------------------------------------------------------
sub _getSlave_status {

#-------------------------------------------------------------------------------
# Description:
# Input Parms:
# Output Parms:
#-------------------------------------------------------------------------------
    my $self = shift;
    $self->ConnectDB();
    my $stmt = q{ show slave status };
    my $sth  = $self->{DBH}->prepare($stmt);
    $sth->execute();
    $self->{slave_status} = $sth->fetchrow_hashref();
    $sth->finish();
    $self->DisconnectDB();
    return $self;
}

1;
__END__
 
=pod
 
=head1 NAME
 
TABLESPACE_USAGE.pm - monitors tablespace usage 
 
=head1 SYNOPSIS
 
=head1 DESCRIPTION
 
=head1 PREREQUISITES
 
The follwing CPAN modules needs to be installed:
 
C<Log::Log4perl>
C<XML::Simple>
C<base qw(moniCA::DBH::MySQL)>
 
=head1 CONFIGURATION FILE
 
  By default
 
=head2 EXAMPLES
 
=head3 example of configuation file
 
Modul           MONITORING::DB::MYSQL::REPLICATION
Logger          MAIL
LogLevel        INFO
Desc            moniCA Infomail / MySQL Replication 

<DBConnect>
        ConnectString   dbi:mysql:database=test;host=127.0.0.1;port=3306
        dbuser          moniCA
        dbpw            moniCA
        description     mysql test db
</DBConnect>

ScheduleInterval	=	900
Report  			=	FALSE
max_event_count		=	2
max_repl_delay		=	60



 
=cut
